'use strict';

const Controller = require('egg').Controller;

class SearchController extends Controller {
  async index() {
    const { ctx } = this;
    var {keyword} = ctx.request.query
    var db = this.app.mongodb;
    var type = ['box','switchs','gmk','banner','group','discount']
    var arr =[]
    // var reg = new RegExp(`${keyword}`)
    for(let i = 0 ; i < type.length ; i++){
      var res = await db.collection(type[i]).find({name:{$regex:eval(`/${keyword}/`)}}).toArray()
      if(res.length){
        arr.push(...res)
      }
    }
    if(arr.length){
      ctx.body = {
        statu :200,
        arr
      }
    }else{
      ctx.body = {
        statu :400,
        msg:"数据库中不存在"
      }
    }
  }
}

module.exports = SearchController;
